@page "/project/list"
@using FOPS.Abstract.MetaInfo.Entity
@using FOPS.Abstract.MetaInfo.Server
@using FOPS.Abstract.K8S.Enum
@using Microsoft.AspNetCore.Components
@using FOPS.Abstract.K8S.Entity
@using FOPS.Abstract.Docker.Entity
@using FOPS.Abstract.Docker.Server
@using FOPS.Abstract.K8S.Server
@using FS.Extends
@using Newtonsoft.Json
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;

<div class="layui-card">
    <div class="layui-card-header" id="anchor-play-header">
        项目
        <select style="display:inline;width: 200px;" @onchange="GroupChange">
            <option value="0">全部</option>
            @foreach (var clusterVO in _lstGroup)
            {
                <option value="@clusterVO.Id">@clusterVO.Name</option>
            }
        </select>
        <div class="layui-right">
            <button class="layui-btn layui-btn-right" @onclick=GotoAdd><i class="am-icon-plus"></i> 添加项目</button>
        </div>
    </div>
    <div class="layui-card-body">
        <div class="layui-form layui-border-box layui-table-view" lay-filter="LAY-table-1" style=" ">
            <div class="layui-table-box">
                <div class="layui-table-header">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <thead>
                        <tr>
                            <th data-field="ID">
                                <div class="layui-table-cell laytable-cell-14-0-0">
                                    <span>ID</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-1">
                                    <span>项目名称</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-2">
                                    <span>项目组</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-2">
                                    <span>Git</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-2">
                                    <span>镜像仓库</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-2">
                                    <span>Dockerfile</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-3">
                                    <span>操作</span>
                                </div>
                            </th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div class="layui-table-body layui-table-main">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <tbody>
                        @for (var index = 0; index < _lst.Count; index++)
                        {
                            var info = _lst[index];
                            <tr data-index="@index">
                                <td>
                                    <div class="layui-table-cell laytable-cell-14-0-0"> @info.Id</div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-1">
                                        <a href="/project/update/@info.Id">@info.Name</a>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-2">
                                        <select @bind="info.GroupId" style="display:block;">
                                            <option value="0">不需要</option>
                                            @foreach (var projectGroupVO in _lstGroup)
                                            {
                                                <option value="@projectGroupVO.Id">@projectGroupVO.Name</option>
                                            }
                                        </select>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-2">
                                        <select @bind="info.GitId" style="display:block;">
                                            <option value="0">不需要</option>
                                            @foreach (var gitVO in _lstGit)
                                            {
                                                <option value="@gitVO.Id">@gitVO.Name</option>
                                            }
                                        </select>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-2">
                                        <select @bind="info.DockerHub" style="display:block;">
                                            <option value="0">不需要</option>
                                            @foreach (var dockerHubVO in _lstDockerHub)
                                            {
                                                <option value="@dockerHubVO.Id">@dockerHubVO.Name</option>
                                            }
                                        </select>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-2">
                                        <select @bind="info.DockerfileTpl" style="display:block;">
                                            <option value="0">不需要</option>
                                            @foreach (var dockerfileTplVO in _lstDockerfileTpl)
                                            {
                                                <option value="@dockerfileTplVO.Id">@dockerfileTplVO.Name</option>
                                            }
                                        </select>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-3">
                                        <button class="layui-btn layui-btn-sm" @onclick="() => Save(info.Id)">保存</button>
                                        <button class="layui-btn layui-btn-sm layui-btn-normal" @onclick="() => Copy(info)">复制</button>
                                    </div>
                                </td>
                            </tr>
                        }
                        </tbody>
                    </table>
                </div>
            </div>
            <PageSplit PageIndex="1" PageSize="@_pageSize" TotalCount="@_totalCount" PageUrl="/project/list/{index}"></PageSplit>
        </div>
    </div>
</div>
<style>
    .laytable-cell-14-0-0{ width: 80px; }
    .laytable-cell-14-0-1{ width: 180px; }
    .laytable-cell-14-0-2{ width: 180px; }
    .laytable-cell-14-0-3{ width: 140px; }
    select { width: 150px; }
</style>

@code{

    private int _totalCount;
    private int _pageSize = 999;

    private int _groupId;
    private List<ProjectVO> _lst = new();
    private List<GitVO> _lstGit = new();
    private List<ProjectGroupVO> _lstGroup = new();
    private List<DockerfileTplVO> _lstDockerfileTpl = new();
    private List<DockerHubVO> _lstDockerHub = new();
    private bool _isFirst;

    protected override async Task OnParametersSetAsync()
    {
        if (_isFirst) return;
        _isFirst = true;

        _lstGroup = await _iocManager.Resolve<IProjectGroupService>().ToListAsync()??new();
        _lstGit = await _iocManager.Resolve<IGitService>().ToListAsync()??new();
        _lstDockerfileTpl = await _iocManager.Resolve<IDockerfileTplService>().ToListAsync()??new();
        _lstDockerHub = await _iocManager.Resolve<IDockerHubService>().ToListAsync()??new();

        await GetList();
    }

    private async Task Save(int id)
    {
        var projectVO = _lst.Find(o => o.Id == id);
        await _iocManager.Resolve<IProjectService>().UpdateAsync(id, projectVO);
        await js.InvokeVoidAsync("layer.msg", "保存成功", new {icon = 1});
    }

    private async Task GroupChange(ChangeEventArgs args)
    {
        _groupId = args.Value.ConvertType(0);
        await GetList();
    }

    private void GotoAdd()
    {
        nav.NavigateTo("/project/add");
    }

    private async Task GetList()
    {
    // 重新读取所有项目列表
        _lst = await _iocManager.Resolve<IProjectService>().ToListAsync()??new();

    // 如果项目组不包含当前选中的组，则移除项目
        if (_groupId > 0)
        {
            _lst.RemoveAll(o => o.GroupId != _groupId);
        }

        _lst = _lst.OrderByDescending(o => o.Id).ToList();
        _totalCount = _lst.Count;
    }

    /// 复制项目
    private async Task Copy(ProjectVO info)
    {
        var newInfo=JsonConvert.DeserializeObject<ProjectVO>(JsonConvert.SerializeObject(info));
        newInfo.Name += "复制";
        newInfo.Id = 0;
        newInfo.DicClusterVer = new();
        newInfo.GitPullAt=DateTime.MinValue;
        var id=await _iocManager.Resolve<IProjectService>().AddAsync(newInfo);
        nav.NavigateTo($"/project/update/{id}");
    }

}